use dta\Turnout_District_Level, clear 

sort year PR_district
drop _merge
merge year PR_district using dta\AlternativeMargin
drop if _merge==2 /* PR-districts not in sample */

gen d2127=0
replace d2127=1 if year==1921|year==1924|year==1927

egen avgC1=mean(C1), by(PR_district d2127)
egen avgC2=mean(C2), by(PR_district d2127)
egen avgmin_distance=mean(min_distance), by(PR_district d2127)
egen avgmin_distance1=mean(min_distance1), by(PR_district d2127)
egen avgmin_distance_iXv_i=mean(min_distance_iXv_i), by(PR_district d2127)
egen avgmin_distance1_iXv_i=mean(min_distance1_iXv_i), by(PR_district d2127)

gen Margin=SMD_margin_pre
gen Margin2=SMD_margin_pre^2

tab PR_district, gen(d_PR_district)

gen ch_margin=avgmin_distance1-Margin


**********************************************
/* EXTENSIONS WITH NOP AND MAGNITUDE */
**********************************************

gen zz=1

drop _merge
sort year SMD_district
merge year SMD_district using dta/NoP_pre.dta


foreach year in 1921 1924 1927 {
drop _merge
sort year PR_district
merge year PR_district using dta/NoP`year'.dta, update
}

keep if zz==1 /* 92 districts with data pre and post reform */

sort SMD_district year
gen ch_NoP=NoP-l3.NoP

gen MarginXch_NoP=Margin*ch_NoP
gen MarginXch_magnitude=Margin*ch_magnitude

******************************CH-RUNNING****************************** 

replace runningOTH=runningOTH+runningT+runningKIR if year<1920
drop runningT runningKIR

foreach party in RF FV H H_FV SP OTH DNA V V_RF NSA NKP NS{
replace running`party'=0 if running`party'==.
}

gen runningLEFT=runningDNA+runningNSA
gen runningCENTER=runningV+runningRF+runningV_RF
gen runningRIGHT=runningH+runningH_FV+runningFV

foreach party in RF FV H H_FV SP OTH DNA V V_RF NSA NKP NS CENTER LEFT RIGHT{
replace running`party'=1 if running`party'==2
replace running`party'=1 if running`party'==3
gen ch_running`party'=running`party'-l3.running`party'
}

rename ch_runningSP ch_runningFARM
drop ch_runningNKP ch_runningNS
**************************************************

gen ch_NoB=ch_runningLEFT+ ch_runningCENTER+ ch_runningRIGHT+ ch_runningFARM+ ch_runningOTH 

gen MarginXch_NoB=Margin*ch_NoB




**************************************************************************
/* TABLE A.1. */
**************************************************************************

eststo clear
reg ch_turnout Margin ch_NoP d_PR_* if year==1921, robust
eststo
reg ch_turnout Margin ch_NoB d_PR_* if year==1921, robust
eststo
reg ch_turnout Margin ch_NoP d_PR_* if year==1921, cluster(PR_district)
eststo
reg ch_turnout Margin ch_NoB d_PR_* if year==1921, cluster(PR_district)
eststo
esttab using tables\TableA1_i_ii.tex, replace cells(b(fmt(3)) se(par fmt(3))) r2 star(* 0.10 ** 0.05 *** 0.01) ///
nodepvars nomtitles prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  


*********************************************************************************************************************
***************************** USING ONLY 1918 for COMPUTING MARGIN **************************************************
*********************************************************************************************************************

use dta\Turnout_District_Level, clear 
gen Margin=l3.margin
gen Margin2=Margin^2

tab PR_district, gen(d_PR_district)

/* 1921 BASED REGRESSIONS */

eststo clear
reg ch_turnout Margin d_PR* if year==1921, robust
eststo
reg ch_turnout Margin d_PR* if year==1921, robust cluster(PR_district)
eststo
esttab using tables\TableA1_iii.tex, replace cells(b(fmt(3)) se(par fmt(3))) r2 star(* 0.10 ** 0.05 *** 0.01) ///
nodepvars nomtitles prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  

*********************************************************************************************************************
***************************** USING FINAL ROUND MARGIN **************************************************
*********************************************************************************************************************

use dta\Turnout_District_Level_ALT_margin, clear 
gen Margin=SMD_margin_pre
gen Margin2=SMD_margin_pre^2
tab PR_district, gen(d_PR_district)

eststo clear
reg ch_turnout Margin d_PR* if year==1921, robust
eststo
reg ch_turnout Margin d_PR* if year==1921, cluster(PR_district)
eststo
esttab using tables\TableA1_iv.tex, replace cells(b(fmt(3)) se(par fmt(3))) r2 star(* 0.10 ** 0.05 *** 0.01) ///
nodepvars nomtitles prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  


*********************************************************************************************************************
***************************** USING ONLY FIRST ROUND TURNOUT ********************************************************
*********************************************************************************************************************

use dta\Turnout_District_Level_ALT, clear 
gen Margin=SMD_margin_pre
gen Margin2=SMD_margin_pre^2
tab PR_district, gen(d_PR_district)

eststo clear
reg ch_turnout Margin d_PR* if year==1921, robust
eststo
reg ch_turnout Margin d_PR* if year==1921, robust cluster(PR_district)
eststo
esttab using tables\TableA1_v.tex, replace cells(b(fmt(3)) se(par fmt(3))) r2 star(* 0.10 ** 0.05 *** 0.01) ///
nodepvars nomtitles prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  


*********************************************************************************************************************
***************************** average turnout pre and post reform  **************************************************
*********************************************************************************************************************

use dta\Turnout_District_Level, clear 
gen Margin=SMD_margin_pre

gen d2127=0
replace d2127=1 if year==1921|year==1924|year==1927

tab PR_district, gen(d_PR_district)

egen turnout_mean=mean(turnout), by(SMD_district d2127)

sort SMD_district year
gen ch_turnout_mean=turnout_mean-l3.turnout_mean

eststo clear
reg ch_turnout_mean Margin d_PR_* if year==1921, robust
eststo
reg ch_turnout_mean Margin d_PR_* if year==1921, cluster(PR_district)
eststo
esttab using tables\TableA1_vi.tex, replace cells(b(fmt(3)) se(par fmt(3))) r2 star(* 0.10 ** 0.05 *** 0.01) ///
nodepvars nomtitles prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  
